#include "RelOp.h"

void SelectPipe::Run(Pipe &inPipe, Pipe &outPipe, CNF &selOp, Record &literal){

    in = &inPipe;
    out = &outPipe;
    cnf = &selOp;
    buffer = &literal;

    pthread_create(&thread, NULL, SelectPipeWorker, (void*)this);
}

void SelectPipe::WaitUntilDone(){
    in->ShutDown();
    pthread_join(thread, NULL);
}

void SelectPipe::Use_n_Pages(int n){
    nOfPages = n;
}

void SelectPipe::DoWork(){
    Record temp;
    ComparisonEngine e;

	while(in->Remove(&temp))
	{
        if(e.Compare(&temp, buffer, cnf))
            out->Insert(&temp);
	}

    out->ShutDown();
}

void *SelectPipeWorker(void *param){
     SelectPipe selectPipe = *((SelectPipe*) param);
     selectPipe.DoWork();
}
